//
//  BannerDetailViewController.m
//  Xspcf
//
//  Created by 小小算盘 on 16/5/16.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "BannerDetailViewController.h"
#import "SunLoginVC.h"
#import "SunProductDetailVC.h"
@interface BannerDetailViewController ()<UIWebViewDelegate,WKNavigationDelegate>
{
    WKWebView *_web;
}
@end

@implementation BannerDetailViewController



    

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"详情";
    if (self.barTitle) {
        self.navigationItem.title = self.barTitle;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    
    [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [backButton sizeToFit];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -10;
    
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    self.navigationItem.leftBarButtonItems = @[negativeSpacer,[[UIBarButtonItem alloc]initWithCustomView:backButton]];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadWebView];
}

- (void)goBack{
    if ([_web canGoBack]) {
        [_web goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)loadWebView
{
    NSURL *strUrl = [[NSURL alloc] init];
    
    NSString *str = @"CZfSDQzsuDayjfzE";
    NSString *str1 =@"12345678";
    
    NSString *token_id=[Utility encryptStr:[NSString stringWithFormat:@"%@",[Sun getUser:USER]]  Key:str1 IV:nil];
    //MD5
    
   
    
    
       if ([self.bannerUrl isEqualToString:KActivity]) {
           self.type = @"5";
           NSString *md5Str=[Utility md5:[NSString stringWithFormat:@"source=ios&token_id=%@&type=%@&%@",token_id,self.type,str]];
        strUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@?source=ios&token_id=%@&type=%@&sign=%@",[NSString stringWithFormat:@"%@/draw/wheel",KWAP],token_id,self.type,md5Str]];
    }else{
        self.type = @"0";
        NSString *md5Str=[Utility md5:[NSString stringWithFormat:@"source=ios&token_id=%@&type=%@&%@",token_id,self.type,str]];
    strUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@?source=ios&token_id=%@&type=%@&sign=%@",self.bannerUrl,token_id,self.type,md5Str]];
    }
    
  if ([[[UIDevice currentDevice] systemVersion] floatValue]<8.0) {
        UIWebView *web =  [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH - 64)];
      web.delegate = self;
        [self.view addSubview:web];
         NSURLRequest *request = [NSURLRequest requestWithURL:strUrl];
        [web loadRequest:request];
    }else{

   NSURLRequest *request = [NSURLRequest requestWithURL:strUrl];

        _web = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH - 64)];
        _web.navigationDelegate = self;
        
    [self.view addSubview:_web];
        [_web loadRequest:request];
   }
    
}
#pragma UIWebViewDelegate
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *str = [NSString stringWithFormat:@"%@",request.URL];
    if ([[[[[str componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"."] firstObject] isEqualToString:@"goToLogin"]) {
        SunLoginVC *lvc = [[SunLoginVC alloc] init];
        lvc.isWeb = YES;
//        lvc.states = 2;
        [self.navigationController pushViewController:lvc animated:YES];
        return NO;
    }else if ([[[[[str componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"."] firstObject] isEqualToString:@"goToProductList"]){
        
        self.tabBarController.selectedIndex = 1;
        [self.navigationController popViewControllerAnimated:YES];
        return NO;
    }else if ([[[[[str componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"."] firstObject] isEqualToString:@"goToProductDetail"]){
        // webToProductDetail
        SunProductDetailVC *productDetail = [[SunProductDetailVC alloc]init];
        NSString *borrowId = [[str componentsSeparatedByString:@"="] lastObject];
        productDetail.isWeb = YES;
        productDetail.borrowId = borrowId;
    }


    return YES;
}
#pragma WKNavigationDelegate
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
}
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
}

-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSLog(@"===%@",navigationAction.request.URL);
    NSString *str = [NSString stringWithFormat:@"%@",navigationAction.request.URL];
    if ([[[[[str componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"."] firstObject] isEqualToString:@"goToLogin"]) {
        SunLoginVC *lvc = [[SunLoginVC alloc] init];
        lvc.isWeb = YES;
//        lvc.states = 2;
        [self.navigationController pushViewController:lvc animated:YES];
        decisionHandler(WKNavigationActionPolicyCancel);
    }
    else if ([[[[[str componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"."] firstObject] isEqualToString:@"goToProductList"]){
        
        self.tabBarController.selectedIndex = 1;
        [self.navigationController popViewControllerAnimated:YES];
        decisionHandler(WKNavigationActionPolicyCancel);
    }else if ([[[[[str componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"."] firstObject] isEqualToString:@"goToProductDetail"]){
        // webToProductDetail
        SunProductDetailVC *productDetail = [[SunProductDetailVC alloc]init];
        NSString *borrowId = [[str componentsSeparatedByString:@"="] lastObject];
        productDetail.isWeb = YES;
        productDetail.borrowId = borrowId;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}
@end
